package services

import (
	"encoding/json"
	"gorm.io/gorm"
	"mygo/app/models"
	"mygo/app/tools"
	"strings"
)

func HandelNj(forecast *models.Forecast, time string, db *gorm.DB) {
	timeArr := strings.Split(time, "-")
	year := timeArr[0]
	month := timeArr[1]
	day := timeArr[2]
	//该日期的前一天
	for i, report := range forecast.Reports {
		if i == 1 {
			for j := 2; j < 5; j++ {
				y, m, d := tools.GetNextDay(year, month, day, j-1)
				report.Data[j][0].Value = m + "月" + d + "日"
				//timeNew := y + "年" + m + "月" + d + "日"
				//月和日补0
				if len(m) == 1 {
					m = "0" + m
				}
				if len(d) == 1 {
					d = "0" + d
				}
				timeNew := y + "-" + m + "-" + d
				//这部分操作与上传文件有关，时间那些
				var tide models.TwcTide
				db.Where("date = ? AND station = ?", timeNew, "鳌江").First(&tide)
				if tide.Id != 0 {
					if tide.TimeHigh1 != "" {
						//report.Data[j][1].Value = tide.TimeHigh1
						timeArr := strings.Split(tide.TimeHigh1, ":")
						report.Data[j][1].Value = timeArr[0] + "点" + timeArr[1] + "分"
						report.Data[j][2].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideHigh1)/100, 2) + "米"
						report.Data[j][3].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideHigh1)/100+3.19, 2) + "米"
					}
					if tide.TimeHigh2 != "" {
						//report.Data[j][4].Value = tide.TimeHigh2
						timeArr := strings.Split(tide.TimeHigh2, ":")
						report.Data[j][4].Value = timeArr[0] + "点" + timeArr[1] + "分"
						report.Data[j][5].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideHigh2)/100, 2) + "米"
						report.Data[j][6].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideHigh2)/100+3.19, 2) + "米"
					}

					if tide.TimeLow1 != "" {
						//report.Data[j][7].Value = tide.TimeLow1
						timeArr := strings.Split(tide.TimeLow1, ":")
						report.Data[j][7].Value = timeArr[0] + "点" + timeArr[1] + "分"
						report.Data[j][8].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideLow1)/100, 2) + "米"
						report.Data[j][9].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideLow1)/100+3.19, 2) + "米"
					}

					if tide.TimeLow2 != "" {
						//report.Data[j][10].Value = tide.TimeLow2
						timeArr := strings.Split(tide.TimeLow2, ":")
						report.Data[j][10].Value = timeArr[0] + "点" + timeArr[1] + "分"
						report.Data[j][11].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideLow2)/100, 2) + "米"
						report.Data[j][12].Value = tools.Float64ToFixedStr(tools.StrToFloat64(tide.TideLow2)/100+3.19, 2) + "米"
					}
				} else {
					for k := 1; k < 13; k++ {
						report.Data[j][k].Value = ""
					}
				}
			}
		} else {
			var foreList models.ForeList
			db.Where("code = ?", "rahx").First(&foreList)
			var foreDetail models.ForeDetailJson
			//db.Where("foreListId = ? AND time = ? AND state != ?", foreList.Id, time, "0").First(&foreDetail)
			if db.Where("foreListId = ? AND time = ? AND state != ?", foreList.Id, time, "0").First(&foreDetail).RowsAffected > 0 {
				var raData = models.Forecast{}
				json.Unmarshal(foreDetail.Data, &raData)
				//fmt.Println(raData.Reports[])
				for j := 1; j < 4; j++ {
					waveArr := tools.GetNumFloat64(raData.Reports[0].Data[j+3][4].Value)
					waveArr = tools.SortFloat64(waveArr)
					str1 := ""
					str2 := ""
					if len(waveArr) == 2 {
						str2 = tools.Float64ToFixedStr(waveArr[0], 1) + "-" + tools.Float64ToFixedStr(waveArr[1], 1) + "米"
						waveLevel1 := getWaveGrade(waveArr[0])
						waveLevel2 := getWaveGrade(waveArr[1])
						if waveLevel1 == waveLevel2 {
							str1 = waveLevel1
						} else {
							str1 = waveLevel1 + "到" + waveLevel2
						}
					} else if len(waveArr) == 1 {
						str2 = tools.Float64ToFixedStr(waveArr[0], 1) + "米"
						str1 = getWaveGrade(waveArr[0])
					}
					if strings.Contains(str1, "到") {
						str1 = strings.Replace(str1, "浪", "", 1)
					}
					report.Data[j][2].Value = "白天" + str2 + "的" + str1
					report.Data[j][3].Value = raData.Reports[0].Data[j+3][7].Value
				}
			}

		}
	}
}
